import static org.hamcrest.CoreMatchers.instanceOf;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.ldap.SortKey;

import com.damon.entity.PrintDeviceEntity;
import com.damon.utils.DbUtil;
import com.damon.utils.GlobalOjbectUtil;

public class PrintNumber {
	public static void main(String[] args) {
		String sql = "select id,device_number,ip,port from print_device where is_connection=1";
		Connection connection = DbUtil.getConnection();
		Statement statement = null;
		ResultSet resultSet;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while(resultSet.next()) {
				PrintDeviceEntity deviceInfo = new PrintDeviceEntity();
				deviceInfo.setId(resultSet.getInt("id"));
				deviceInfo.setDeviceNumber(resultSet.getString("device_number"));
				deviceInfo.setIp(resultSet.getString("ip"));
				deviceInfo.setPort(resultSet.getInt("port"));
				GlobalOjbectUtil.getDeviceList().add(deviceInfo);
			}
		} catch (SQLException e) {
//			logger.error("查询设备表出错！！");
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println(GlobalOjbectUtil.getDeviceList());
		for(PrintDeviceEntity deviceEntity : GlobalOjbectUtil.getDeviceList()){
			Socket socket=null;
			OutputStream os = null;
			try {
				 socket = new Socket(deviceEntity.getIp(),deviceEntity.getPort());
				 os = socket.getOutputStream();
				String msg = "^XA" + 
						"^CI28^CW1,E:SIMSUN.FNT" + 
						"^MD15^PW440^LL735^FO193,340^GB121,0,1^FS^FO193,540^GB244,0,1^FS^FO313,1^GB0,540,1^FS^FO193,1^GB0,734,1^FS" + 
						"^FO313,1^GB127,160,127^FS" + 
						"^FT340,20^A1R,100,124^FR^FD包^FS" + 
						"^FT404,160^A1R,30,30^FH\\^FD目的地:^FS" + 
						"^FT340,170^A1R,43,33^FH\\^FD下关中央北路^FS" + 
						"^FT270,22^A1R,30,30^FH\\^FD始发地:^FS" + 
						"^FT217,22^A1R,40,40^FH\\^FD南京转运场^FS" + 
						"^FT275,350^A1R,30,30^FH\\^FD件数:^FS" + 
						"^FT275,440^A1R,40,40^FH\\^FD13^FS" + 
						"^FT220,350^A1R,30,30^FH\\^FD格口:^FS" + 
						"^FT220,440^A1R,40,40^FH\\^FD13DI^FS" + 
						"^FT360,545^A1R,30,30^FH\\^FD简码:^FS" + 
						"^FT260,545^A1R,60,60^FH\\^FDd876^FS" + 
						"^BY3,3,130^FT40,35^BCR,,Y,N" + 
						"^FD>:B>520102123014727>62T>59999>69^FS" + 
						"^PQ1,0,1,Y^XZ";
				os.write(msg.getBytes("utf-8"));
			} catch (Exception e) {
				System.out.println("连接失败！");
			}finally {
				if (socket != null) {
					try {
						socket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		}
	}
}
